﻿namespace YunQue.Services
{
    /// <summary>
    /// 通用服务基类
    /// </summary>
    public class YunQueBaseService
    {
        /// <summary>
        /// 用户上下文
        /// </summary>
        protected readonly UserContext _userContext;
        /// <summary>
        /// 日志帮助类
        /// </summary>
        protected readonly ILogger<YunQueBaseService> _logger;
        /// <summary>
        /// 数据库工厂，一般用来创建其他tenant的数据库上下文
        /// </summary>
        protected readonly IDbContextFactory _dbContextFactory;

        /// <summary>
        /// 数据库上下文
        /// </summary>
        protected YunQueDbContext _dbContext;
        /// <summary>
        /// 请求取消令牌
        /// </summary>
        protected readonly CancellationToken _cancellationToken;

        private readonly IServiceProvider _serviceProvider;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="serviceProvider"></param>
        public YunQueBaseService(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            _userContext = _serviceProvider.GetRequiredService<UserContext>();
            _logger = _serviceProvider.GetRequiredService<ILogger<YunQueBaseService>>();
            _dbContextFactory= _serviceProvider.GetRequiredService<IDbContextFactory>();
            _dbContext = _serviceProvider.GetRequiredService<YunQueDbContext>();
            _cancellationToken = _serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext.RequestAborted;
        }
    }
}
